package com.hngates.project.controller;

import java.util.List;

import com.hngates.common.utils.ShiroUtils;
import com.hngates.project.domain.IdName;
import com.hngates.project.domain.SiteInformation;
import com.hngates.project.domain.dto.SiteInformationDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hngates.common.annotation.Log;
import com.hngates.common.enums.BusinessType;
import com.hngates.project.domain.ConstructionUnit;
import com.hngates.project.service.IConstructionUnitService;
import com.hngates.common.core.controller.BaseController;
import com.hngates.common.core.domain.AjaxResult;
import com.hngates.common.utils.poi.ExcelUtil;
import com.hngates.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 单位信息Controller
 *
 * @author ruoyi
 * @date 2022-06-24
 */
@Controller
@RequestMapping("/project/constructionUnit")
public class ConstructionUnitController extends BaseController {
    private String prefix = "project/constructionUnit";

    @Autowired
    private IConstructionUnitService constructionUnitService;

    @RequiresPermissions("project:constructionUnit:view")
    @GetMapping()
    public String constructionUnit() {
        return prefix + "/constructionUnit";
    }

    /**
     * 查询单位信息列表
     */
    @RequiresPermissions("project:constructionUnit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ConstructionUnit constructionUnit) {
        startPage();
        List<ConstructionUnit> list = constructionUnitService.selectConstructionUnitList(constructionUnit);
        return getDataTable(list);
    }

    /**
     * 导出单位信息列表
     */
    @RequiresPermissions("project:constructionUnit:export")
    @Log(title = "单位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ConstructionUnit constructionUnit) {
        List<ConstructionUnit> list = constructionUnitService.selectConstructionUnitList(constructionUnit);
        ExcelUtil<ConstructionUnit> util = new ExcelUtil<ConstructionUnit>(ConstructionUnit.class);
        return util.exportExcel(list, "单位信息数据");
    }

    /**
     * 下载模板
     *
     * @return
     */
    @RequiresPermissions("project:constructionUnit:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ConstructionUnit> util = new ExcelUtil<>(ConstructionUnit.class);
        return util.importTemplateExcel("单位信息数据");
    }

    /**
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @RequiresPermissions("project:constructionUnit:import")
    @PostMapping("/importConstructionUnit")
    @ResponseBody
    public AjaxResult importConstructionUint(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<ConstructionUnit> util = new ExcelUtil<>(ConstructionUnit.class);
        List<ConstructionUnit> constructionUnitList = util.importExcel(file.getInputStream(), 1);
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = constructionUnitService.importConstructionUnit(constructionUnitList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 新增单位信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存单位信息
     */
    @RequiresPermissions("project:constructionUnit:add")
    @Log(title = "单位信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ConstructionUnit constructionUnit) {
        return toAjax(constructionUnitService.insertConstructionUnit(constructionUnit));
    }

    /**
     * 修改单位信息
     */
    @RequiresPermissions("project:constructionUnit:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ConstructionUnit constructionUnit = constructionUnitService.selectConstructionUnitById(id);
        mmap.put("constructionUnit", constructionUnit);
        return prefix + "/edit";
    }

    /**
     * 修改保存单位信息
     */
    @RequiresPermissions("project:constructionUnit:edit")
    @Log(title = "单位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ConstructionUnit constructionUnit) {
        return toAjax(constructionUnitService.updateConstructionUnit(constructionUnit));
    }

    /**
     * 删除单位信息
     */
    @RequiresPermissions("project:constructionUnit:remove")
    @Log(title = "单位信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(constructionUnitService.deleteConstructionUnitByIds(ids));
    }

    /**
     * 查询Id Name
     * @return
     */
    @GetMapping("/queryUnitIdName")
    @ResponseBody
    public AjaxResult queryUnitIdName() {
        List<IdName> idNames = constructionUnitService.selectunitId();
        return AjaxResult.success(idNames);
    }
}
